import { Component, OnInit } from '@angular/core';
import { IndexDbHelpService } from 'src/app/services/IndexDbHelp.service';

@Component({
  selector: 'app-index-db-demo',
  templateUrl: './index-db-demo.component.html',
  styleUrls: ['./index-db-demo.component.scss']
})
export class IndexDbDemoComponent implements OnInit {

  options1: any = {
    width: "120px",
    text: "创建IndexDb"
  };
  options2: any = {
    width: "120px",
    text: "新增一条"
  };
  options3: any = {
    width: "120px",
    text: "修改数据"
  };
  options4: any = {
    width: "120px",
    text: "按ID查询"
  };
  options5: any = {
    width: "120px",
    text: "查询全部"
  };
  options6: any = {
    width: "120px",
    text: "按ID删除"
  };
  options7: any = {
    width: "120px",
    text: "删除数据库"
  };
  constructor(private indexDb: IndexDbHelpService) { }

  ngOnInit() {
  }

  //创建IndexDb
  btnClick1() {
    this.indexDb.createIndexDB("test", 1, "PopupListElement", "Id", [{ Id: "001", PopupListId: "1001", Type: "IsLastNode", value: "Y", name: "是" }, { Id: "000", PopupListId: "1001", Type: "IsLastNode", value: "N", name: "否" }]);
  }

  //新增一条
  btnClick2() {
    this.indexDb.addEntity("test", 1, "PopupListElement", { Id: "003", PopupListId: "1001", Type: "IsLastNode", value: "YN", name: "是否" })
  }

  //修改数据
  btnClick3() {
    this.indexDb.updateEntity("test", 1, "PopupListElement", { Id: "001", PopupListId: "1001", Type: "IsLastNode", value: "Y", name: "是" })
      .then(function (data) {
        console.log(data);
      }, function (error) {
        console.log(error);
      });
  }

  //按ID查询
  btnClick4() {
    this.indexDb.getEntityForKey("test", 1, "PopupListElement", "001")
      .then(function (data) {
        console.log(data);
      }, function (error) {
        console.log(error);
      });
  }

  //查询全部
  btnClick5() {
    this.indexDb.getAllByStorage("test", 1, "PopupListElement")
      .then(function (data) {
        console.log(data);
      }, function (error) {
        console.log(error);
      });
  }

  //按ID删除
  btnClick6() {
    this.indexDb.deleteEntityByKey("test", 1, "PopupListElement","003")
      .then(function (data) {
        console.log(data);
      }, function (error) {
        console.log(error);
      });
  }

  //删除数据库
  btnClick7() {
    this.indexDb.deleteDatabase("test");
  }

}
